<template>
	<div class="system-user-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="450px" :close-on-click-modal="false">
			<el-form ref="userDialogFormRef" :model="state.ruleForm" size="default" label-width="90px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="头像">
							<el-upload
								:show-file-list="false"
								:auto-upload="false"
								accept= "image/jpeg, image/png, image/jpg"
								:on-change="onChange">
								<img v-if="state.ruleForm.user_img" :src="state.VITE_IMG_URL + state.ruleForm.user_img" class="my-avatar" />
								<div v-else class="my-upload-avatar">
									<SvgIcon name="ele-Plus" :size="20"/>
								</div>
							</el-upload>
						</el-form-item>
					</el-col>
					<el-col :xs="24" class="mb20">
						<el-form-item label="账号">
							<el-input v-model="state.ruleForm.user_name" placeholder="请输入账号" clearable :disabled="state.dialog.type === 'edit'"></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" class="mb20">
						<el-form-item label="昵称">
							<el-input v-model="state.ruleForm.nickname" placeholder="请输入昵称" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" class="mb20">
						<el-form-item label="关联角色">
							<el-select v-model="state.ruleForm.auth_id" placeholder="请选择" clearable class="w100">
								<el-option v-for="(item,index) in state.roleData" :key="item.id" :label="item.role_name" :value="item.id"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" class="mb20">
						<el-form-item label="密码">
							<el-input v-model="state.ruleForm.password" placeholder="请输入密码" autocomplete="new-password" type="password" show-password></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" class="mb20">
						<el-form-item label="用户状态">
							<el-switch v-model="state.ruleForm.status" :active-value="1" :inactive-value="0" inline-prompt active-text="启" inactive-text="禁"></el-switch>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
	<CropperDialog ref="cropperDialogRef" @refresh_img="refresh_img"/>
</template>

<script setup name="systemUserDialog">
import { reactive, ref, defineAsyncComponent } from 'vue';
import { adminApi } from '/@/api/admin/index';

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);

// 引入组件
const CropperDialog = defineAsyncComponent(() => import('./cropper.vue'));

// 定义变量内容
const cropperDialogRef = ref();

// 定义变量内容
const userDialogFormRef = ref();
const ruleForm = {
	user_img:'',
	user_name: '', // 账户名称
	password: '', // 用户昵称
	nickname: '', // 关联角色
	user_img: '', // 部门
	auth_id:'',//角色id
	status: 1, // 用户状态
}
const state = reactive({
	VITE_IMG_URL:import.meta.env.VITE_IMG_URL,
	ruleForm,
	roleData: [], // 角色数据
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
});
const refresh_img = (url) => {
	state.ruleForm.user_img = url;
}
/**
 * 文件选择改变时触发
 */
const onChange = (file, fileListVal) => {
	if (file.status !== "ready") return;
	onCropperDialogOpen(file.raw)
}
// 打开裁剪弹窗
const onCropperDialogOpen = (file) => {
	cropperDialogRef.value.openDialog(file);
};

// 打开弹窗
const openDialog = (type, row) => {
	state.dialog.type = type
	if (type === 'edit') {
		state.ruleForm = JSON.parse(JSON.stringify(row));
		state.dialog.title = '修改用户';
		state.dialog.submitTxt = '修 改';
	} else {
		state.dialog.title = '新增用户';
		state.dialog.submitTxt = '新 增';
		state.ruleForm = ruleForm
	}
	state.dialog.isShowDialog = true;
};

// 关闭弹窗
const closeDialog = () => {
	state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
	closeDialog();
};
// 提交
const onSubmit = () => {
	if (state.dialog.type === 'add') { 
		adminApi().add(state.ruleForm).then(res=>{
			closeDialog();
			emit('refresh');
		})
	}else{
		adminApi().edit(state.ruleForm).then(res=>{
			closeDialog();
			emit('refresh');
		})
	}
};

// 暴露变量
defineExpose({
	openDialog,
	state
});
</script>
<style  scoped lang="scss">
	:deep(.el-form-item--default .el-form-item__content) {
		line-height: 0;
	}
</style>